package com.mediacloud.app.newsmodule.view

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import android.view.animation.Animation
import android.widget.ViewFlipper

class XViewFlipper(context: Context?, attrs: AttributeSet?) : ViewFlipper(context, attrs) {
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
    }

    override fun stopFlipping() {
        super.stopFlipping()
    }

    override fun removeViewInLayout(view: View?) {
        super.removeViewInLayout(view)
    }

    override fun showPrevious() {
        super.showPrevious()
    }

    override fun removeViews(start: Int, count: Int) {
        super.removeViews(start, count)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
    }

    override fun setOutAnimation(outAnimation: Animation?) {
        super.setOutAnimation(outAnimation)
    }

    override fun setOutAnimation(context: Context?, resourceID: Int) {
        super.setOutAnimation(context, resourceID)
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
    }

    override fun setFlipInterval(milliseconds: Int) {
        super.setFlipInterval(milliseconds)
    }

    override fun isFlipping(): Boolean {
        return super.isFlipping()
    }

    override fun removeView(view: View?) {
        super.removeView(view)
    }

    override fun removeViewAt(index: Int) {
        super.removeViewAt(index)
    }

    override fun removeViewsInLayout(start: Int, count: Int) {
        super.removeViewsInLayout(start, count)
    }

    override fun startFlipping() {
        super.startFlipping()
    }

    override fun setDisplayedChild(whichChild: Int) {

        val count = childCount
        var mWhichChild = whichChild
        if (whichChild >= childCount) {
            mWhichChild = 0
        } else if (whichChild < 0) {
            mWhichChild = childCount - 1
        }
        val hasFocus = focusedChild != null
        var childIndex=mWhichChild
        for (i in 0 until count) {
            val child = getChildAt(i)
            if (i == childIndex)
            {
                /*if (animate && mInAnimation != null) {
                    child.startAnimation(mInAnimation)
                }*/
//                child.visibility = View.VISIBLE
//                mFirstTime = false
//                Log.w("APPTAG","current   : $i")
            }
            else
            {
                /*if (animate && mOutAnimation != null && child.visibility == View.VISIBLE) {
                    child.startAnimation(mOutAnimation)
                } else if (child.animation === mInAnimation)
                    child.clearAnimation()*/
//                child.visibility = View.GONE
//                Log.w("APPTAG","other  childindex: $i")
            }
        }
        super.setDisplayedChild(whichChild)
        var location = IntArray(2)
        getLocationOnScreen(location)
    }

    override fun showNext() {
        if(isShown&&getLocalVisibleRect(Rect(0,0,context.resources.displayMetrics.widthPixels,context.resources.displayMetrics.heightPixels)))
            super.showNext()
    }

    override fun setAnimateFirstView(animate: Boolean) {
        super.setAnimateFirstView(animate)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)

        val count = childCount
        var mWhichChild = displayedChild
        if (displayedChild >= childCount) {
            mWhichChild = 0
        } else if (displayedChild < 0) {
            mWhichChild = childCount - 1
        }
        val hasFocus = focusedChild != null
        var childIndex=mWhichChild
        for (i in 0 until count)
        {
            val child = getChildAt(i)
            if (i == childIndex)
            {
                /*if (animate && mInAnimation != null) {
                    child.startAnimation(mInAnimation)
                }*/
//                child.visibility = View.VISIBLE
//                mFirstTime = false
//                Log.w("APPTAG","onLayout - childindex: $i")
            }
            else
            {
                if(child.animation==outAnimation)
                /*if (animate && mOutAnimation != null && child.visibility == View.VISIBLE) {
                    child.startAnimation(mOutAnimation)
                } else if (child.animation === mInAnimation)
                    child.clearAnimation()*/
                    child.visibility = View.GONE
//                Log.w("APPTAG","onLayout  other  childindex: $i")
            }
        }
    }

}